$(function(){

	$("#recipecategory-index .allselect").toggle(function(){
		$("#recipecategory-index input[type=checkbox]").attr('checked', 'checked');
	},function(){
		$("#recipecategory-index input[type=checkbox]").attr('checked',null);
	});

	$("#recipecategory-index tbody tr").hover(function(){
		$(this).css("background-color", "#d9e6ff");
	}, function(){
		$(this).css("background-color", "white");
	});

	$("#recipecategory-index .buttondel").click(function(){
		var datas = $("#recipecategory-index form.delete").serializeArray();
		if(datas==''){
			alert('请选择要删除的分类');
		}
		else{
			if(window.confirm('确认删除这些分类?')){
				$("#recipecategory-index form.delete").submit();
			}
		}
	});

	$("#recipecategory-index form.search").submit(function(){
		var key = $("#recipecategory-index input[name=key]").val();
		if(key==''){
			alert('搜索分类不能为空');
			return false;
		}
	});

	$("#recipecategory-select form.search").submit(function(){
		var key = $("#recipecategory-select input[name=key]").val();
		if(key==''){
			alert('搜索分类不能为空');
			return false;
		}
	});

	$("#recipecategory-select tbody tr").each(function(){
		var tr = $(this);
		tr.click(function(){
			$("#recipecategory-select tbody tr").removeClass("addcolor");
			tr.addClass("addcolor");
			tr.find("[type='radio']").attr("checked","checked");
			$("#recipecategory-select [name='getname']").val(tr.find(".name").text());
			$("#recipecategory-select [name='sid']").val(tr.find(":radio").val());
		});
	});

	$("#recipecategory-select form.addname").submit(function(){
		var key = $("#recipecategory-select input[name=getname]").val();
		if(key==''){
			alert('请选择分类');
			return false;
		}
	});

	var text = $("#recipecategory-add-edit .pic");
	var button = $("#recipecategory-add-edit form .upload");
	var fileType = "pic",fileNum = "one";

	new AjaxUpload(button,{
		action: APP+'/RecipeCategory/uploads',
		name: 'pic',
		onSubmit : function(file, ext){
			if(fileType == "image")
			{
				if (ext && /^(jpg|png|jpeg|gif)$/.test(ext)){
					this.setData({
						'info': '文件类型为图片'
					});
				} else {
					alert('非图片类型文件，请重传');
					return false;
				}
			}
		},
		onComplete: function(file, response){
			if(isNaN(response[0])){
				alert(response);

			}else{
				text.val(response);
				$("#recipecategory-add-edit .img img").attr('src', PUBLIC+"/Content/tmp/"+response);
			}
		}
	});


});